<template>
  <main class="oj-main">
    <!-- 英雄区域 -->
    <section class="hero">
      <div class="hero-content">
        <h1>提升编程技能，从这里开始</h1>
        <p>thousands 道编程题目，实时评测，助你成长</p>
        <div class="hero-btns">
          <button class="btn primary" @click="goToProblems">开始刷题</button>
          <button class="btn secondary" @click="goToContests">参加比赛</button>
        </div>
      </div>
    </section>

    <!-- 数据概览 -->
    <section class="stats">
      <div class="stat-item">
        <span class="stat-number">{{ problemCount }}</span>
        <span class="stat-label">编程题目</span>
      </div>
      <div class="stat-item">
        <span class="stat-number">{{ userCount }}</span>
        <span class="stat-label">注册用户</span>
      </div>
      <div class="stat-item">
        <span class="stat-number">{{ submissionCount }}</span>
        <span class="stat-label">代码提交</span>
      </div>
    </section>

    <!-- 推荐题目 -->
    <section class="problems">
      <div class="section-header">
        <h2>推荐题目</h2>
        <button class="more-btn" @click="goToProblems">查看全部</button>
      </div>
      <div class="problem-list">
        <div
          class="problem-card"
          v-for="(problem, index) in recommendedProblems"
          :key="index"
        >
          <h3 class="problem-title">{{ problem.title }}</h3>
          <div class="problem-info">
            <span :class="`difficulty ${problem.difficulty}`">{{
              problem.difficulty
            }}</span>
            <span class="acceptance">{{ problem.acceptance }}</span>
          </div>
          <button class="solve-btn" @click="solveProblem(problem.id)">
            解题
          </button>
        </div>
      </div>
    </section>

    <!-- 最近比赛 -->
    <section class="contests">
      <div class="section-header">
        <h2>最近比赛</h2>
        <button class="more-btn" @click="goToContests">查看全部</button>
      </div>
      <div class="contest-list">
        <div
          class="contest-card"
          v-for="(contest, index) in recentContests"
          :key="index"
        >
          <h3 class="contest-title">{{ contest.title }}</h3>
          <div class="contest-time">
            <i class="fa fa-clock-o"></i>
            <span>{{ contest.time }}</span>
          </div>
          <button class="join-btn" @click="joinContest(contest.id)">
            报名
          </button>
        </div>
      </div>
    </section>
  </main>
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import { languages } from "monaco-editor";
import typescript = languages.typescript;

// 模拟数据 - 题目
interface Problem {
  id: number;
  title: string;
  difficulty: "简单" | "中等" | "困难";
  acceptance: string;
}

// 模拟数据 - 比赛
interface Contest {
  id: number;
  title: string;
  time: string;
}

// 状态数据
const problemCount = ref("1,200+");
const userCount = ref("8,500+");
const submissionCount = ref("120,000+");

// 推荐题目
const recommendedProblems = ref<Problem[]>([
  { id: 1001, title: "两数之和", difficulty: "简单", acceptance: "52.3%" },
  {
    id: 1021,
    title: "删除最外层的括号",
    difficulty: "简单",
    acceptance: "74.1%",
  },
  {
    id: 2003,
    title: "二叉树的层序遍历",
    difficulty: "中等",
    acceptance: "64.5%",
  },
]);

// 最近比赛
const recentContests = ref<Contest[]>([
  { id: 1, title: "周赛 #65", time: "2023-06-15 19:00" },
  { id: 2, title: "算法基础挑战赛", time: "2023-06-20 14:00" },
]);

// 方法
const goToProblems = () => {
  console.log("跳转到题目列表");
  // 实际项目中使用路由跳转
  // router.push('/problems');
};

const goToContests = () => {
  console.log("跳转到比赛列表");
  // router.push('/contests');
};

const solveProblem = (id: number) => {
  console.log(`开始解答题目 ${id}`);
  // router.push(`/problem/${id}`);
};

const joinContest = (id: number) => {
  console.log(`报名比赛 ${id}`);
  // router.push(`/contest/${id}`);
};

// 数字增长动画
onMounted(() => {
  // 简单的数字增长动画效果
  const animateValue = (
    el: HTMLElement,
    start: number,
    end: number,
    duration: number
  ) => {
    let startTimestamp: number | null = null;
    const step = (timestamp: number) => {
      if (!startTimestamp) startTimestamp = timestamp;
      const progress = Math.min((timestamp - startTimestamp) / duration, 1);
      el.innerHTML =
        Math.floor(progress * (end - start) + start).toLocaleString() + "+";
      if (progress < 1) {
        window.requestAnimationFrame(step);
      }
    };
    window.requestAnimationFrame(step);
  };

  // 获取统计数字元素并应用动画
  const stats = document.querySelectorAll(".stat-number");
  if (stats.length >= 3) {
    animateValue(stats[0] as HTMLElement, 0, 1200, 1500);
    animateValue(stats[1] as HTMLElement, 0, 8500, 1500);
    animateValue(stats[2] as HTMLElement, 0, 120000, 1500);
  }
});
</script>

<style>
.oj-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

/* 英雄区域 */
.hero {
  background: linear-gradient(135deg, #165dff 0%, #722ed1 100%);
  color: white;
  padding: 60px 20px;
  border-radius: 10px;
  margin-bottom: 40px;
  text-align: center;
}

.hero-content h1 {
  font-size: 2.2rem;
  margin-bottom: 15px;
}

.hero-content p {
  font-size: 1.1rem;
  margin-bottom: 30px;
  opacity: 0.9;
}

.hero-btns {
  display: flex;
  gap: 15px;
  justify-content: center;
}

/* 按钮样式 */
.btn {
  padding: 10px 20px;
  border-radius: 5px;
  border: none;
  font-size: 1rem;
  cursor: pointer;
  transition: all 0.2s;
}

.primary {
  background: white;
  color: #165dff;
  font-weight: bold;
}

.secondary {
  background: rgba(255, 255, 255, 0.2);
  color: white;
}

.btn:hover {
  transform: translateY(-2px);
}

/* 数据概览 */
.stats {
  display: flex;
  justify-content: space-around;
  margin-bottom: 50px;
  text-align: center;
}

.stat-item {
  padding: 20px;
}

.stat-number {
  display: block;
  font-size: 2rem;
  font-weight: bold;
  color: #165dff;
  margin-bottom: 8px;
}

.stat-label {
  color: #666;
}

/* 通用区域标题 */
.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.section-header h2 {
  font-size: 1.5rem;
  color: #333;
}

.more-btn {
  color: #165dff;
  background: none;
  border: none;
  cursor: pointer;
  font-size: 0.9rem;
}

/* 题目列表 */
.problems {
  margin-bottom: 50px;
}

.problem-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.problem-card {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  transition: all 0.2s;
}

.problem-card:hover {
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
  transform: translateY(-3px);
}

.problem-title {
  font-size: 1.1rem;
  margin-bottom: 15px;
  color: #333;
}

.problem-info {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  font-size: 0.9rem;
}

.difficulty {
  padding: 3px 8px;
  border-radius: 4px;
  font-size: 0.8rem;
}

.简单 {
  background: #e6f7e9;
  color: #00875a;
}

.中等 {
  background: #fff8e6;
  color: #d48806;
}

.困难 {
  background: #ffe6e6;
  color: #bf2626;
}

.acceptance {
  color: #666;
}

.solve-btn {
  width: 100%;
  padding: 8px;
  background: #f0f7ff;
  color: #165dff;
  border: 1px solid #cce0ff;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.solve-btn:hover {
  background: #e6f0ff;
}

/* 比赛列表 */
.contests {
  margin-bottom: 50px;
}

.contest-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.contest-card {
  border: 1px solid #eee;
  border-radius: 8px;
  padding: 20px;
  transition: all 0.2s;
}

.contest-card:hover {
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.08);
  transform: translateY(-3px);
}

.contest-title {
  font-size: 1.1rem;
  margin-bottom: 15px;
  color: #333;
}

.contest-time {
  color: #666;
  font-size: 0.9rem;
  margin-bottom: 15px;
  display: flex;
  align-items: center;
  gap: 5px;
}

.join-btn {
  width: 100%;
  padding: 8px;
  background: #f0f7ff;
  color: #165dff;
  border: 1px solid #cce0ff;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.2s;
}

.join-btn:hover {
  background: #165dff;
  color: white;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .stats {
    flex-direction: column;
    gap: 20px;
  }

  .problem-list,
  .contest-list {
    grid-template-columns: 1fr;
  }
}
</style>
